package com.blast.comicsisland.model.myview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.GridView;

/**
 * Created by Android Studio
 * Author: hg
 * Date: 15/6/26
 * Email: 531057892@qq.com
 */
public class MyGridView extends GridView {
    public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int hMode = MeasureSpec.getMode(heightMeasureSpec);

        int hSize = MeasureSpec.getSize(heightMeasureSpec);

        String mode = "";

        switch (hMode){
            case MeasureSpec.AT_MOST: // 对应 wrap_content
                mode = "AT_MOST";
                break;
            case MeasureSpec.EXACTLY:
                mode = "EXACTLY";
                break;
            case MeasureSpec.UNSPECIFIED:
                mode = "UNSPECIFIED";
                break;
        }

        Log.d("FullListView", "h: " + hSize + " mode: " + mode);

        // 重新生成 一个高度的规范，避免 ListView 只显示一行
        // 因为 ScrollView 传递 heightMeasureSpec时，模式 时 UNSPECIFIED
        // ListView 强制显示一行。
        // 下面代码，强制改成AT_MOST即可。
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
